package portfolio;

import java.util.Date;

public class Page {
	
	private int index;
	private String owner,
					fileType,
					original,
					thumb,
					title,
					medium,
					awards,
					comments;
	private Date date;
	
	private String dfs = "---"; 				//Default value for empty or blank strings
	private String dft = "default_thumb.jpg"; 	//Default location of the thumb nail
	private String dfo = "default.jpg";			//Default location of the original media
	private String dff = "jpg";
	
	/**
	 * This constructor is <b>required</b> for this Class to exist.
	 * It is also recommended you change the {@link #setDefaults(String, String, String, String)}
	 * 
	 * @see #setDefaults(String, String, String, String)
	 * @see #isValid()
	 * 
	 * @param index the index reference of the page. <b>Must</b> be unique
	 * @param owner the name of the artist
	 * @param fileType the file extension of the media
	 * @param original the file location of the original media
	 * @param thumb the file location of the thumb nail image
	 * @param title the title of the media
	 * @param date the date of the media. Should not be NULL, but not required.
	 * @param medium the medium the media was created in
	 * @param awards any awards this media has won
	 * @param comments any comments by the author(s)
	 */
	public Page(int index, String owner, String fileType, String original,
			String thumb, String title, Date date, String medium, String awards,
			String comments) {
		super();
		this.index = index;
		this.owner = owner;
		this.fileType = fileType;
		this.original = original;
		this.thumb = thumb;
		this.title = title;
		this.date = date;
		this.medium = medium;
		this.awards = awards;
		this.comments = comments;
	}
	
	/**
	 * @param string the default message for <i>owner</i>, <i>title</i>, <i>medium</i>, <i>awards</i>, and <i>comments</i>,
	 * @param thumb the default location of the thumb nail
	 * @param original the default location of a picture
	 * @param file the default file type. Should be the same file type original uses. 
	 */
	public void setDefaults(String string, String thumb, String original, String file){
		dfs = string;
		dft = thumb;
		dfo = original;
		dff = file;
	}

	public void setOwner(String owner) {
		// Leave original owner alone if incoming value is empty.
		if(owner.isEmpty() && !this.owner.isEmpty())
			return;
		
		this.owner = owner;
	}

	public void setFileType(String fileType) {
		// Leave original file extension alone if incoming value is empty
		if(fileType.isEmpty() && !this.fileType.isEmpty())
			return;
		
		this.fileType = fileType;
	}

	public void setOriginal(String original) {
		// Leave the original media location alone if the incoming value is empty
		if(original.isEmpty() && !this.original.isEmpty())
			return;
		
		this.original = original;
	}

	public void setThumb(String thumb) {
		// Leave the thumb nail location alone if the incoming value is empty
		if(thumb.isEmpty() && !this.thumb.isEmpty())
			return;
		
		this.thumb = thumb;
	}

	public void setTitle(String title) {
		// Leave the title alone if the incoming value is empty
		if(title.isEmpty() && !this.title.isEmpty())
			return;
		
		this.title = title;
	}

	public void setMedium(String medium) {
		// Leave the medium alone if the incoming value is empty
		if(medium.isEmpty() && !this.medium.isEmpty())
			return;
		
		this.medium = medium;
	}

	public void setAwards(String awards) {
		// Leave awards alone if the incoming value is empty
		if(awards.isEmpty() && !this.awards.isEmpty())
			return;
		
		this.awards = awards;
	}

	public void setComments(String comments) {
		// Leave comments alone if the incoming value is empty
		if(comments.isEmpty() && !this.comments.isEmpty())
			return;
		
		this.comments = comments;
	}

	public void setDate(Date date) {
		// Leave the date alone if the incoming value is empty
		if(date == null && this.date != null)
			return;
		
		this.date = date;
	}

	public int getIndex() {
		if(index < 0)
			throw new IndexOutOfBoundsException("\"" + index + "\" is not a valid index value. Must be >= 0");
		else
			return index;
	}

	public String getOwner() {
		if(owner == null || owner.isEmpty())
			return dfs;
		else 
			return owner;
	}

	public String getFileType() {
		if(!fileType.isEmpty())
			return fileType;
		
		if(original.isEmpty())
			return dff;
		
		String[] extention = original.split(".");
		return extention[extention.length-1];
	}

	public String getOriginal() {
		if(original == null || original.isEmpty())
			return dfo;
		else
			return original;
	}

	public String getThumb() {
		if(thumb == null || thumb.isEmpty())
			return dft;
		else
			return thumb;
	}

	public String getTitle() {
		if(title == null)
			return dfs;
		else if(title.isEmpty())
			return dfs;
		else 
			return title;
	}

	public String getMedium() {
		if(medium == null || medium.isEmpty())
			return dfs;
		else 
			return medium;
	}

	public String getAwards() {
		if(awards == null || awards.isEmpty())
			return dfs;
		else 
			return awards;
	}

	public String getComments() {
		if(comments == null || comments.isEmpty())
			return dfs;
		else 
			return comments;
	}

	public Date getDate() {
		if(date == null || date == null)
			throw new NullPointerException();
		else
			return date;
	}
	
	/**
	 * Checks to see if two Pages have the same {@link #getIndex()}. All other
	 * attributes are ignored.
	 * 
	 * @param other the {@link Page} you wish to compare with
	 * @return TRUE if the index of the two pages match. FALSE otherwise.
	 */
	public boolean equals(Page other){
		if(other.getIndex() == this.getIndex())
			return true;
		else
			return false;
	}
	
	/**
	 * Overloads the compareTo
	 * 
	 * @param other compares two (@link {@link #getIndex()}. Used for sorting
	 * @return the difference between two Pages
	 */
	public int compareTo(Page other){
		return this.getIndex() - other.getIndex();
	}
	
	/**
	 * @return
	 */
	public boolean isValid(){
		
		return !(index <= 0 ||
				owner.isEmpty() ||
				fileType.isEmpty() ||
				original.isEmpty() ||
				thumb.isEmpty() ||
				title.isEmpty());
	}
	
	public String toString(){
		try{
			return String.format(	"Page Inxed:%1$4d                 | Title: %2$-30s\n" +
								"Owner:     %3$-20s | Date:     %4$tm/%4$te/%4$tY\n" +
								"Type:      %5$-20s | Medium:   %6$-30s\n" +
								"Original:  %7$-20s | Awards:   %8$-30s\n" +
								"Thumb:     %9$-20s | Comments: %10$-30s"
									, index, 
									title, 
									owner, 
									date,  
									fileType, 
									medium, 
									original, 
									awards, 
									thumb, 
									comments);
		} catch (NullPointerException e){
			return String.format(	"Page Inxed:%4d                 | Title: %-30s\n" +
					"Owner:     %-20s | Date:     ---\n" +
					"Type:      %-20s | Medium:   %-30s\n" +
					"Original:  %-20s | Awards:   %-30s\n" +
					"Thumb:     %-20s | Comments: %-30s"
						, index, 
						title, 
						owner, 
						date,  
						fileType, 
						medium, 
						original, 
						awards, 
						thumb, 
						comments);
		}
	}
}
